
package com.feib.soeasy.action;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;


/**
 * @title (#)Soez1005ActionBean.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2011/1/7 create by Jimmy Liu<br>
 */
public class Soez1005ActionBean extends BaseActionBean {
    

    private static final String DEFAULT_JSP = "/WEB-INF/views/soez1005/soez1005.jsp";
    
    @SpringBean("fileDownloadDestination")
    private String fileDownloadDestination;
    
    private String fileName = "遠東商銀全方位企業收款服務繳費說明.pdf";
    
    private String errorMsg = null;

    @DontBind
    @DefaultHandler
    public Resolution doDefault()
    {       
        logger.debug("Enter doDefault mehtod.");
        FileInputStream fis = null;
        try
        {
            String dir = fileDownloadDestination + File.separator + "SOEZ1005"+ File.separator;
            File file = new File(dir);
            if (!file.exists())
                FileUtils.forceMkdir(new File(dir));
                        
            fis = new FileInputStream(dir + fileName);
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            String isoFileName = new String(fileName.getBytes(), "ISO8859-1");
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
            StreamingResolution resolution = null;
            resolution = new StreamingResolution("application/pdf",inputStream);
            resolution.setFilename(isoFileName);
            return resolution;            
        }
        catch(Exception ex)
        {
            logger.error("下載繳費說明發生錯誤：", ex);
            errorMsg = "繳費說明檔案不存在，請聯絡網站管理員";
            return new ForwardResolution(DEFAULT_JSP);
        }
        finally
        {
            if (null != fis)
            {
                try
                {
                    fis.close();
                }
                catch (IOException e)
                {
                    // donothing.
                }
            }
        }
    }

    
    /**
     * @return the errorMsg
     */
    public String getErrorMsg() {
        return errorMsg;
    }

    
    /**
     * @param errorMsg the errorMsg to set
     */
    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }
}
